import pandas as pd
import numpy as np
def cut_and_agg(path):
df = pd.read_csv(path)
df['bin'] = pd.cut(df.timestamp, [i for i in np.arange(0, 240.25, 0.25)])
df = df.groupby(['bin', 'particle_no']).agg({
'x':['mean'],
'y':['mean'],
'z':['mean']})
df.columns = ['x', 'y', 'z']
return df.reset_index()
files = ["adaptiverk4-test1.csv",
"adaptiverk4-test2.csv",
"adaptiverk4-test3.csv",
"heun-test1.csv",
"heun-test2.csv",
"heun-test3.csv",
"rk4-test1.csv",
"rk4-test2.csv",
"rk4-test3.csv",
"rk5-test1.csv",
"rk5-test2.csv",
"rk5-test3.csv"
]
for file in files:
exec(f"{file.split('.')[0].replace('-', '_')} = cut_and_agg('{file}')")
adaptiverk4_test1
| bin | particle_no | x | y | z | |
|---|---|---|---|---|---|
| 0 | (0.0, 0.25] | 0 | -4.000000e+00 | 4.000000 | 0 |
| 1 | (0.0, 0.25] | 1 | -3.000000e+00 | 3.997839 | 0 |
| 2 | (0.0, 0.25] | 2 | -2.000000e+00 | 3.997837 | 0 |
| 3 | (0.0, 0.25] | 3 | -1.000000e+00 | 3.997837 | 0 |
| 4 | (0.0, 0.25] | 4 | -4.052738e-08 | 3.997837 | 0 |
| ... | ... | ... | ... | ... | ... |
| 61435 | (239.75, 240.0] | 59 | -9.638859e-01 | -4.901562 | 0 |
| 61436 | (239.75, 240.0] | 60 | 6.964504e-02 | -4.896837 | 0 |
| 61437 | (239.75, 240.0] | 61 | 1.082531e+00 | -4.703087 | 0 |
| 61438 | (239.75, 240.0] | 62 | 2.049363e+00 | -4.390655 | 0 |
| 61439 | (239.75, 240.0] | 63 | 2.995046e+00 | -4.058573 | 0 |
61440 rows × 5 columns
# def calc_distance(x1,y1,z1,x2,y2,z2):
# dx = x2-x1
# dy = y2-y1
# dz = z2-z1
# return ((dx)**2+ (dy)**2 + (dz)**2)**(0.5)
def calc_error(df1, df2):
df1_copy = df1.copy()
df2_copy = df2.copy()
df2_copy.columns = [str(i)+"2" for i in df2_copy.columns]
df = pd.concat([df1_copy, df2_copy], axis=1)
df['error'] = df.apply(lambda row: np.linalg.norm(np.array([row.x, row.y, row.z])-np.array([row.x2, row.y2, row.z2])), axis=1)
df = df.groupby('bin').agg({
"error": "mean"}).reset_index()
df = df[['bin', 'error']]
return df
for file in (files):
exec(f"{file.split('.')[0].replace('-', '_') + '_error'} = calc_error({file.split('.')[0].replace('-', '_')}, {'rk5_' + file.split('.')[0].split('-')[1]})")
print(file.split('.')[0].replace('-', '_') + '_error')
adaptiverk4_test1_error adaptiverk4_test2_error adaptiverk4_test3_error heun_test1_error heun_test2_error heun_test3_error rk4_test1_error rk4_test2_error rk4_test3_error rk5_test1_error rk5_test2_error rk5_test3_error
adaptiverk4_test1_error
| bin | error | |
|---|---|---|
| 0 | (0.0, 0.25] | 4.380328e-11 |
| 1 | (0.25, 0.5] | 4.052426e-10 |
| 2 | (0.5, 0.75] | 1.271275e-08 |
| 3 | (0.75, 1.0] | 3.085828e-09 |
| 4 | (1.0, 1.25] | 1.151678e-08 |
| ... | ... | ... |
| 955 | (238.75, 239.0] | 3.798489e-07 |
| 956 | (239.0, 239.25] | 4.111348e-07 |
| 957 | (239.25, 239.5] | 3.644810e-07 |
| 958 | (239.5, 239.75] | 3.594737e-07 |
| 959 | (239.75, 240.0] | 2.858367e-07 |
960 rows × 2 columns
import seaborn as sns
errors = [
"adaptiverk4_test1_error",
"adaptiverk4_test2_error",
"adaptiverk4_test3_error",
"heun_test1_error",
"heun_test2_error",
"heun_test3_error",
"rk4_test1_error",
"rk4_test2_error",
"rk4_test3_error"]
for error in errors:
exec(f"{error}['x']={error}.apply(lambda row: str(row.name), axis=1)")
print(f"sns.lineplot(data={error}, x='x', y='error')")
sns.lineplot(data=adaptiverk4_test1_error, x='x', y='error') sns.lineplot(data=adaptiverk4_test2_error, x='x', y='error') sns.lineplot(data=adaptiverk4_test3_error, x='x', y='error') sns.lineplot(data=heun_test1_error, x='x', y='error') sns.lineplot(data=heun_test2_error, x='x', y='error') sns.lineplot(data=heun_test3_error, x='x', y='error') sns.lineplot(data=rk4_test1_error, x='x', y='error') sns.lineplot(data=rk4_test2_error, x='x', y='error') sns.lineplot(data=rk4_test3_error, x='x', y='error')
sns.lineplot(data=adaptiverk4_test1_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>
sns.lineplot(data=adaptiverk4_test2_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>
sns.lineplot(data=adaptiverk4_test3_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>
sns.lineplot(data=heun_test1_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>
sns.lineplot(data=heun_test2_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>
sns.lineplot(data=heun_test2_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>
sns.lineplot(data=heun_test3_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>
sns.lineplot(data=rk4_test1_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>
sns.lineplot(data=rk4_test2_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>
sns.lineplot(data=rk4_test3_error, x='x', y='error')
<AxesSubplot:xlabel='x', ylabel='error'>